// pages/shop_course/index.js
import {navTo,navToTab,onInput,onClick,showText} from '../../utils/reuse'
import{addCourseTime,getCourseTimeList,removeUserTime} from '../../api/index'
import {formatTime} from '../../utils/util'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import {baseURL} from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    course_show:false,
    form:{
      name:'',
      price:'',
      course:''
    },
    fileList: [],
    fileUrl:[],

    // 营业时间
    business_show:false,
    currentDate1: '请选择',
    currentDate2: '请选择',
    minHour: 0,
    maxHour: 23,

    page:1,
    pagesize:10,
    page_show:true,
    page_text:'加载中...',
    timer1: null 
  },
  removeFn(e){
    Dialog.confirm({
      title: '确定要删除吗？'
    })
      .then((res) => {
        const id = e.currentTarget.dataset.id
        var obj = {
          uid:wx.getStorageSync('uid'),
          time_id:id,
        }
        removeUserTime(obj).then(res=>{
          if(res.sta == 1){
            showText('删除成功！',1)
            this.setData({
              page:1,
              pagesize:this.data.pagesize,
              page_show:true,
              list:[]
            })
            this.getCourseList()
          }else{
            showText('删除失败！',2)
          }
        })
      })
      .catch((res) => {
        console.log(res)
      });
  },
  getCourseList(){
    var obj = {
      uid:wx.getStorageSync('uid'),
      shop_id:wx.getStorageSync('shop_id'),
    }
    getCourseTimeList(obj).then(res=>{
      if(res.sta == 1){
        this.setData({
          list:res.msg
        })
      }else{
        if(res.msg){
          showText('服务器异常，请稍后重试！',2)
        }
      }
    })
  },
  submit(){
    var obj = {
      uid:wx.getStorageSync('uid'),
      shop_id:wx.getStorageSync('shop_id'),
      starttime:this.data.currentDate1,
      endtime:this.data.currentDate2
    }
    addCourseTime(obj).then(res=>{
      if(res.sta == 1){
        showText(res.msg,1)
        this.setData({
          page:1,
          pagesize:this.data.pagesize,
          page_show:true,
          list:[]
        })
        this.getCourseList()
        this.setData({
          course_show:false
        })
      }else if(res.sta == 0){
        showText(res.msg,2)
        this.setData({
          course_show:false
        })
      }else{
        showText(res.msg,2)
      }
      // if(res.m)
    })
  },
  timeChange(e){
    
  },
  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  timeChange1(e){
      this.setData({
        currentDate1:e.detail.value,
      })
  },
  timeChange2(e){
      this.setData({
        currentDate2:e.detail.value
      })
  },
  cancel(e){
    this.setData({
      form:{
        name:'',
        price:'',
        course:'',
        // brief:''
      },
      course_show:false
    })
  },
  deleteFn(e){
    var num1 = this.data.fileList.splice(1,e.detail.index)
    var num2 = this.data.fileUrl.splice(1,e.detail.index)
    this.setData({
      fileList:num1,
      fileUrl:num2,
    })
  },
  click(e){
    onClick(e,this)
  },
  change(e){
    onInput(e,this)
  },
  input(key){
    onInput(key,this)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCourseList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getCourseList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})